﻿#region Copyright Statement
//  ------------------------------------------------------------------------------
//  
//    This file is part of The Streambolics Library.
//    Copyright © 2005-2009, Stephan Leclercq & Streambolics
//    All rights reserved.
//  
//    The Streambolics Library is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//  
//    The Streambolics Library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//  
//    You should have received a copy of the GNU General Public License
//    along with Streambolics; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//  
//  ------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

namespace Streambolics
{
    public class IniFile : DataFile
    {
        private Dictionary<string, IniSection> _Sections = new Dictionary<string, IniSection> ();

        [DllImport ("kernel32")]
        private static extern long WritePrivateProfileString (string section,
            string key, string val, string filePath);
        [DllImport ("kernel32")]
        private static extern int GetPrivateProfileString (string section,
                 string key, string def, StringBuilder retVal,
            int size, string filePath);

        public IniFile (string aName)
            : base (aName)
        {
        }

        public IniSection Section (string aSection)
        {
            string uName = aSection.ToUpper();
            if (_Sections.ContainsKey (uName))
            {
                return _Sections[uName];
            }
            else
            {
                IniSection s = new IniSection (this, aSection);
                _Sections.Add (uName, s);
                return s;
            }
        }

        public IniKey Value (string aSection, string aKey)
        {
            return Section (aSection).Value (aKey);
        }

        public IniSection this[string aSection]
        {
            get
            {
                return Section (aSection);
            }
        }

        public IniKey this[string aSection, string aKey]
        {
            get
            {
                return Value (aSection, aKey);
            }
        }

        public IEnumerable<IniSection> Sections
        {
            get
            {
                foreach (string s in SectionNames)
                {
                    yield return Section (s);
                }
            }
        }

        public IEnumerable<string> SectionNames
        {
            get
            {
                //  TODO : Enumerate the sections here...
                yield break;
            }
        }

        public void SetValue (string aSectionName, string aKeyName, string aValue)
        {
            WritePrivateProfileString (aSectionName, aKeyName, aValue, Name);
        }

        public string GetValue (string aSectionName, string aKeyName, string aDefault)
        {
            StringBuilder sb = new StringBuilder (4096);
            GetPrivateProfileString (aSectionName, aKeyName, aDefault, sb, 9999, Name);
            return sb.ToString();
        }

    }



}
